<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="指令代码" prop="zldZldm">
        <el-input
          v-model="queryParams.zldZldm"
          placeholder="请输入指令代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指令名称" prop="zldZlmc">
        <el-input
          v-model="queryParams.zldZlmc"
          placeholder="请输入指令名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="有效" prop="zldFlag">
        <el-select
          v-model="queryParams.zldFlag"
          placeholder="请选择有效"
          clearable
          size="small"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="指令描述" prop="zldDesc">
        <el-input
          v-model="queryParams.zldDesc"
          placeholder="请输入指令描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:zld:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:zld:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:zld:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-copy-document"
          size="mini"
          @click="handleCopy"
          v-hasPermi="['system:zld:add']"
        >
          拷贝
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:zld:export']"
        >
          导出
        </el-button>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="zldList"
      border
      stripe
      highlight-current-row
      :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
      height="100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="指令代码"
        width="80"
        align="center"
        prop="zldZldm"
      />
      <el-table-column label="指令名称" align="center" prop="zldZlmc" />
      <el-table-column label="有效" width="80" align="center" key="zldFlag">
        <template #default="scope">
          <el-switch
            v-model="scope.row.zldFlag"
            active-value="0"
            inactive-value="1"
            @change="handleZldFlag(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="指令描述" align="center" prop="zldDesc" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="更改人" align="center" prop="updateBy" />
      <el-table-column label="更改时间" align="center" prop="updateTime" />
      <el-table-column
        label="操作"
        width="200"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:zld:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:zld:remove']"
          >
            删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAllotUser(scope.row)"
            v-hasPermi="['system:zld:edit']"
          >
            分配用户
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改指令代码对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      width="500px"
      draggable
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="指令代码" prop="zldZldm">
          <el-input v-model="form.zldZldm" placeholder="请输入指令代码" />
        </el-form-item>
        <el-form-item label="指令名称" prop="zldZlmc">
          <el-input v-model="form.zldZlmc" placeholder="请输入指令名称" />
        </el-form-item>
        <el-form-item label="有效">
          <el-radio-group v-model="form.zldFlag">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="指令描述" prop="zldDesc">
          <el-input v-model="form.zldDesc" placeholder="请输入指令描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      v-model="openCopy"
      :close-on-click-modal="false"
      width="500px"
      draggable
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rulesCopy" label-width="80px">
        <el-form-item label="被拷贝者" prop="oldUser">
          <el-input v-model="form.oldUser" placeholder="请输入被拷贝用户账号" />
        </el-form-item>
        <el-form-item label="拷贝者" prop="oldUser">
          <el-input v-model="form.newUser" placeholder="请输入拷贝用户账号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitCopy">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listZld,
  getZld,
  delZld,
  addZld,
  updateZld,
  exportZld,
  changeZldFlag,
  copyUserAuth,
} from "@/api/system/zld";

export default {
  name: "Zld",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 状态数据字典
      statusOptions: [],
      // 总条数
      total: 0,
      // 指令代码表格数据
      zldList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openCopy: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        zldZldm: null,
        zldZlmc: null,
        zldFlag: null,
        zldDesc: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        zldZldm: [
          { required: true, message: "指令代码不能为空", trigger: "blur" },
        ],
        zldZlmc: [
          { required: true, message: "指令名称不能为空", trigger: "blur" },
        ],
        zldFlag: [{ required: true, message: "有效不能为空", trigger: "blur" }],
      },
      rulesCopy: {
        oldUser: [
          { required: true, message: "被拷贝者不能为空", trigger: "blur" },
        ],
        newUser: [
          { required: true, message: "拷贝者不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then((response) => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询指令代码列表 */
    getList() {
      this.loading = true;
      listZld(this.queryParams).then((response) => {
        this.zldList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openCopy = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        zldId: null,
        zldZldm: null,
        zldZlmc: null,
        zldFlag: "0",
        zldDesc: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        oldUser: null,
        newUser: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.zldId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加指令代码";
    },
    handleCopy() {
      this.reset();
      this.openCopy = true;
      this.title = "拷贝指令权限";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const zldId = row.zldId || this.ids;
      getZld(zldId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改指令代码";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.zldId != null) {
            updateZld(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addZld(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitCopy() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          copyUserAuth(this.form).then((res) => {
            this.msgSuccess("拷贝成功");
            this.openCopy = false;
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const zldIds = row.zldId || this.ids;
      this.$confirm(
        '是否确认删除指令代码编号为"' + zldIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delZld(zldIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有指令代码数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportZld(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    handleZldFlag(row) {
      let text = row.zldFlag === "0" ? "启用" : "停用";

      this.$confirm('确认要"' + text + "吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return changeZldFlag(row.zldId, row.zldFlag);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.zldFlag = row.zldFlag === "0" ? "1" : "0";
        });
    },
    handleAllotUser(row) {
      const zldId = row.zldId;
      this.$router.push("/system/zld-auth/zld/" + zldId + "/" + row.zldZldm);
    },
  },
};
</script>
